package com.sndscl.rpc.common.util;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;

import java.util.List;

/**
 * @Description
 * @Author lsh
 * @Date 2020/10/14 13:26
 **/
public class NodeChangeWatcher implements Watcher {

    public ZookeeperClient zookeeperClient;

    public NodeChangeWatcher(ZookeeperClient zookeeperClient){
        this.zookeeperClient = zookeeperClient;
    }

    @Override
    public void process(WatchedEvent watchedEvent) {
        System.out.println("监听路径为：" + watchedEvent.getPath());
        System.out.println("监听的类型为：" + watchedEvent.getType());
        try {
            List<String> list = zookeeperClient.weatherDir("/serverlist",this);
            System.out.println(list);
            for(int i = 0; i < list.size(); i++){
                String c = list.get(i);
                byte[] data = zookeeperClient.getData(watchedEvent.getPath()+"/"+c);
                System.out.println(c + " "+ new String(data));
            }
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
